package com.education.project.web.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.education.project.model.LoginUser;
import com.education.project.model.LoginUserType;
import com.education.project.service.LoginUserService;
import com.education.project.service.LoginUserTypeService;
import com.education.project.service.WebNavResourcesService;
import com.education.project.web.form.SystemLoginForm;

@Controller
public class SystemLoginLogoutController {
	@Autowired
	private LoginUserService loginUserService;
	@Autowired
	private LoginUserTypeService loginUserTypeService;
	@Autowired
	private WebNavResourcesService webNavResourcesService;
	
	@RequestMapping("/blankpage")
	public void getBlankPage(HttpServletResponse response) throws IOException {
		response.getWriter().print("");
		response.getWriter().close();
	}

	@RequestMapping(value="/getimagecode",method=RequestMethod.GET)
	public void getImageCode(HttpServletRequest request, HttpServletResponse response) throws IOException {
		CaptchaUtil.out(120, 35, 5, request, response);
	}

	@RequestMapping(value="/system_login",method=RequestMethod.GET)
	public ModelAndView getLoginPage() {
		ModelAndView model = new ModelAndView("system_login");
		model.addObject("loginUser", new SystemLoginForm());
		return model;
	}
	
	@RequestMapping(value="/system_login",method=RequestMethod.POST)
	public String login(
			@Valid @ModelAttribute("loginUser") SystemLoginForm userForm,
			BindingResult binder,
			HttpServletRequest request) {
		LoginUser loginUser = null;
		if(!binder.hasErrors()) {
			if(!CaptchaUtil.ver(userForm.getImagecode(), request)) {
				binder.rejectValue("imagecode", "error", "验证码错误");
			} else {
				loginUser = loginUserService.getLoginUserByNamePasssword(userForm.getLoginname(), userForm.getPassword());
				if(loginUser==null) {
					binder.rejectValue("loginname", "error", "用户名或密码错误");
				}
			}
		}
		if(binder.hasErrors()) {
			CaptchaUtil.clear(request);
			request.setAttribute("error", true);
			return "system_login";
		}
		LoginUserType loginUserType = loginUserTypeService.getLoginUserType(loginUser.getLoginUserTypeId());
		loginUser.setLoginUserType(loginUserType);
		request.getSession().setAttribute("loginUser", loginUser);
		List navlist = webNavResourcesService.getResourcesByUserType(loginUserType.getId());
		request.setAttribute("navlist", navlist);
		request.setAttribute("update_password", false);
		return "system_main";
	}
	
	@RequestMapping("/system_logout")
	public String logout(HttpSession session) {
		LoginUser loginUser = (LoginUser) session.getAttribute("loginUser");
		if(loginUser!=null) {
			session.removeAttribute("loginUser");
		}
		return "redirect:system_login";
	}
}
